package com.wind.controller;

import com.wind.config.PropConfig;
import com.wind.entity.FileContent;
import com.wind.entity.ReqDataBean;
import com.wind.model.GeoObserveTime;
import com.wind.service.GeoObserveTimeService;
import com.wind.util.DateUtil;
import com.wind.util.FileUtil;
import com.wind.util.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/9/3.
 */
@Controller
@RequestMapping("/geo")
public class GeoCatelogController {

    @Autowired
    PropConfig propConfig;

    @Autowired
    KylinController kylinController;

    @Autowired
    private GeoObserveTimeService geoObserveTimeService;

    private Logger logger = LoggerFactory.getLogger(GeoCatelogController.class);

    List<GeoObserveTime> geoObserveTimeList = new ArrayList<GeoObserveTime>();

    @RequestMapping("/geoObserve")
    public void exeOrb() throws Exception {
        try {
            kylinController.ORB_EPH();
            String beginTime = ReqDataBean.getTaskBeginTime();

            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(beginTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH) + 1;
            logger.info("***************month:"+month);
            Runtime rn = Runtime.getRuntime();
            //rn.exec(propConfig.getORB_EPHlocation() +"/orb_eph.exe",null,new File(propConfig.getORB_EPHlocation()));
            //将ORB_EPH下的eph_eci.txt拷贝到predict下的state.TXT，作为predict.exe的输入文件
            FileUtil.copyFile(propConfig.getORB_EPHlocation() +"/eph_eci.txt",
                    propConfig.getPredictlocation() +"/state.TXT");
            rn.exec(propConfig.getPredictlocation() +"/predict.exe",null,new File(propConfig.getPredictlocation()));
            //month=2;
            initGeoObserveTime();

            if(month==2 || month==3){//降交点
                setDSNodeOrASNodeTime("D");
            }else if(month==10){//升交点
                setDSNodeOrASNodeTime("A");
            }else if(month>=4 && month<=9){//南极点
                setDSNodeOrASNodeTime("S");
            }else{//北极点
                setDSNodeOrASNodeTime("N");
            }

            //除去SSA区
            List<String> saaList = new ArrayList<String>();
            saaList = kylinController.testSAA("0");
            if(saaList.size()>0){
                for (GeoObserveTime geo:geoObserveTimeList){
                    String geoBeginTime = geo.getGeoplanstarttime();
                    String geoEndTime = geo.getGeoplanendtime();

                    for (String str:saaList){
                        if(!DateUtil.chceckDateOfcurrentDate(geoBeginTime,str)
                                && DateUtil.chceckDateOfcurrentDate(geoEndTime,str)){
                            geoObserveTimeList.remove(geo);
                            break;
                        }
                    }
                }
            }

            //将除去SAA部分的数据存入库
            geoObserveTimeService.insertBatchGeoObserveTime(geoObserveTimeList);

        } catch (Exception e) {
            logger.error("exec error",e);
        }
    }

    /**
    *@Description: 根据降交点输出文件获取圈号以及初始化其他固定参数
    * @Param:
    * @return: void
    * @Author: longwj
    * @Date: 2018/9/5 16:09
    */
    public void initGeoObserveTime(){
        if(geoObserveTimeList.size()>0){
            geoObserveTimeList.clear();
        }
        List<FileContent> fcList = Tools.getFileContent(propConfig.getPredictlocation()+"/DSNODE.TXT");
        for(int i=0;i<fcList.size();i++) {
            if ((i - 1) % 5 == 0) { //获取圈号
                GeoObserveTime entity = new GeoObserveTime();
                entity.setGeolatvalue(0.0);
                entity.setGeosensorparam1("1/3");
                entity.setGeosensorparam2("750ms");
                entity.setGeosensorparam3("-30");
                entity.setGeosensorparam4("20");
                int orbitid = Integer.parseInt(fcList.get(i).getContent().trim()) - 1;
                entity.setOrbitid(orbitid);
                entity.setSatelliteid("QS-5A");
                entity.setRegid("00233a9fdba443258b9a12081daade44");
                geoObserveTimeList.add(entity);
            }
        }
    }

    /**
    *@Description: 设置特征点时间点和弧度
    * @Param:  pole:特征点，A-升交点，D-降交点，S-南极点，N-北极点
    * @return:
    * @Author: longwj
    * @Date: 2018/9/5 23:51
    */
    public void setDSNodeOrASNodeTime(String pole){
        List<FileContent> fcList = new ArrayList<FileContent>();
        List<String> poleTimeList = new ArrayList<String>();
        if("A".equals(pole)){//升交点
            fcList=Tools.getFileContent(propConfig.getPredictlocation()+"/ASNODE.TXT");
        }else if("D".equals(pole)){//降交点
            fcList=Tools.getFileContent(propConfig.getPredictlocation()+"/DSNODE.TXT");
        }else {
            fcList=Tools.getFileContent(propConfig.getPredictlocation()+"/MAX_MIN_B.TXT");
            for (FileContent fc:fcList) {
                String[] arr = fc.getContent().trim().split("\\s+");
                if("S".equals(pole) && arr[6].contains("-")){//纬度为负表示南极
                    String effiTime = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5];
                    poleTimeList.add(effiTime.trim());
                }else if("N".equals(pole) && !arr[6].contains("-")){ //纬度为正表示北极
                    String effiTime = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5];
                    poleTimeList.add(effiTime.trim());
                }
            }
        }

        for(int i = 0; i<geoObserveTimeList.size(); i++){
            System.out.println(i);
            String pointTime = "";
            int orbitid = geoObserveTimeList.get(i).getOrbitid();
            switch (pole){
                case "D":
                    geoObserveTimeList.get(i).setAttcontroldyl("JJD");
                    pointTime= fcList.get(orbitid*5-3).getContent().trim();
                    break;
                case "A":
                    geoObserveTimeList.get(i).setAttcontroldyl("SJD");
                    pointTime= fcList.get(orbitid*4-2).getContent().trim();
                    break;
                case "S":
                    geoObserveTimeList.get(i).setAttcontroldyl("NJD");
                    pointTime = poleTimeList.get(i);
                    break;
                case "N":
                    geoObserveTimeList.get(i).setAttcontroldyl("BJD");
                    pointTime = poleTimeList.get(i);
                    break;
                default:
                    break;
            }
            System.out.println("**********pointTime:"+pointTime);
            String effiTime = pointTime;
            if("A".equals(pole) || "D".equals(pole)){
                 effiTime = DateUtil.formatASNSNodeDateTime(pointTime);
            }

            String geoplanstarttime = DateUtil.addSeconds(effiTime, DateUtil.normalTimeFormat,-1350);
            String geoplanendtime = DateUtil.addSeconds(effiTime, DateUtil.normalTimeFormat,1350);
            geoObserveTimeList.get(i).setGeoplanstarttime(geoplanstarttime);
            geoObserveTimeList.get(i).setGeoplanendtime(geoplanendtime);
            geoObserveTimeList.get(i).setAttcontrolptime(effiTime);
        }
    }

    public static void main(String[] args) {
        /*String dateStr = "2018-09-03 16:13:20";
        String pattern = "yyyy-MM-dd HH:mm:ss";
        int seconds1 = 1350;
        int seconds2 = -1350;
        String beginTime = DateUtil.addSeconds(dateStr,pattern,seconds1);
        String endTime = DateUtil.addSeconds(dateStr,pattern,seconds2);
        System.out.println("beginTime:"+beginTime+" endTime:"+endTime);

        String str = DateUtil.addSeconds("2016-09-01 11:47:37.949",DateUtil.normalTimeFormat,0);
        System.out.println("str:"+str);*/

        String a = "2018-11-01 12:20:30";
        String b = "2018-09-10 05:32:15.000";
        boolean b1 = DateUtil.chceckDateOfcurrentDate(a,b);
        System.out.println("bl:"+b1);

    }
}
